package cn.limbo.myandserver.controller;

import android.content.Context;
import cn.limbo.myandserver.dao.DatabaseUtils;
import cn.limbo.myandserver.model.User;
import cn.limbo.myandserver.utils.JsonUtils;
import cn.limbo.myandserver.utils.MD5;
import com.yanzhenjie.andserver.annotation.*;
import com.yanzhenjie.andserver.http.HttpRequest;
import com.yanzhenjie.andserver.http.session.Session;
import com.yanzhenjie.andserver.util.MediaType;

import java.util.ArrayList;

/**
 * 用户登录控制模块
 */

@Controller
@PostMapping("/user")
public class UserController {

    /**
     * 数据库工具对象
     */
    private DatabaseUtils utils;

    /**
     * 用户登录
     * @param account 用户名
     * @param password 密码
     * @param context 上下文对象
     * @param request request对象
     * @return 登录结果
     */
    @GetMapping("/login")
    @ResponseBody
    String login(@RequestParam(name = "account") String account,
                        @RequestParam(name = "password") String password,
                        Context context, HttpRequest request) {

        utils = new DatabaseUtils(context);
        ArrayList<User> users = utils.query();
        for (User user : users) {
            if (user.getAccount().equals(account)) {
                if (user.getPassword().equals(MD5.md5(password))) {
                    Session session = request.getValidSession();
                    session.setAttribute("isLogin", true);
                    return JsonUtils.getJson(0, "登录成功！点击跳转到功能页面！");
                }
                return JsonUtils.getJson(1, "用户名或密码错误，请确认无误后再重新登录！");
            }
        }
        return JsonUtils.getJson(1, "用户名不存在，请注册后再登录！");
    }

    /**
     * 注册功能
     * @param account 用户名
     * @param password 密码
     * @param checkPassword 确认密码
     * @param context 上下文对象
     * @return 注册结果
     */
    @GetMapping(path = "/register", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    @ResponseBody
    public String register(@RequestParam(name = "account") String account,
                         @RequestParam(name = "password") String password,
                         @RequestParam(name = "checkPassword") String checkPassword, Context context) {

        utils = new DatabaseUtils(context);
        String reg_account_1 = "[a-zA-Z0-9_\\u4e00-\\u9fa5\\s·]+";
        String reg_account_2 = "\\d+\\d+\\d";
        String reg_account_3 = "(_)|(__)|(_+)";
        String reg_password = "[\\S]{6,24}";

        if (!account.matches(reg_account_1)){
            return JsonUtils.getJson(1, "用户名不能有特殊字符！");
        } else if (account.matches(reg_account_2)) {
            return JsonUtils.getJson(1, "用户名不能全为数字！");
        } else if (account.matches(reg_account_3)) {
            return JsonUtils.getJson(1, "用户名首尾不能是下划线！");
        } else if (!password.matches(reg_password)) {
            return JsonUtils.getJson(1, "密码不能有空格！");
        } else if (!password.equals(checkPassword)) {
            return JsonUtils.getJson(1, "两次密码不一致！");
        } else {
            long add = utils.add(account, MD5.md5(password));
            if (add == -1) {
                return JsonUtils.getJson(1, "用户名已存在，请重新注册！");
            } else {
                return JsonUtils.getJson(0, "注册成功，请登录！");
            }
        }
    }
}
